<template>
    <div class="home" style="padding: 10px">
        <!--  功能区域  -->
        <div class="flex-bar">
            <!--  搜索区域  -->
            <div style="margin: 10px 10px">
                <p style="font-size: 20px; color: #E6A23C">以下是您的会面信息，请您和{{this.role}}在指定时间会面，并在会面结束后选择是否{{this.confirm}}。</p>
            </div>
        </div>
        <el-table
                :data="tableData"
                border
                stripe
                style="width: 100%">
            <el-table-column
                    label="房屋信息">
                <template #default="scope">
                    <el-button size="mini" type="primary" @click="showHouses(scope.row.houseId)">查看详细信息</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="租客姓名" v-if="this.user.role === 1"></el-table-column>
            <el-table-column prop="name" label="房主姓名" v-if="this.user.role === 2"></el-table-column>
            <el-table-column prop="meetTime" label="会面时间"></el-table-column>
            <el-table-column prop="meetAddress" label="会面地点"></el-table-column>

            <el-table-column label="操作" v-if="this.user.role === 1">
                <template #default="scope">
                    <el-popconfirm title="确认拒绝租出吗？" @confirm="refuse(scope.row.id)">
                        <template #reference>
                            <el-button size="mini" type="danger">拒绝租出</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>

            <el-table-column label="操作" v-if="this.user.role === 2">
                <template #default="scope">
                    <el-popconfirm title="确认租房吗？" @confirm="wantRent(scope.row.houseId)">
                        <template #reference>
                            <el-button size="mini" type="primary">确认租住</el-button>
                        </template>
                    </el-popconfirm>
                    <el-popconfirm title="确认取消租房吗？" @confirm="refuse(scope.row.id, scope.row.houseId)">
                        <template #reference>
                            <el-button size="mini" type="danger">取消租住</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!--  房屋信息弹窗  -->
        <el-dialog title="房屋详细信息" v-model="houseVis" width="50%">
            <el-table :data="houseList" stripe border>
                <el-table-column prop="houseAddress" label="ID"></el-table-column>
                <el-table-column prop="type" label="房屋类型"></el-table-column>
                <el-table-column prop="suitableNumber" label="适住人数"></el-table-column>
                <el-table-column prop="rent" label="月租金"></el-table-column>
                <el-table-column
                        prop="photo"
                        label="房屋照片">
                    <template #default="scope">
                        <el-image
                                style="width: 100px"
                                :src="scope.row.photo"
                                :preview-src-list="[scope.row.photo]">
                        </el-image>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <!--  分页  -->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>

</template>

<style>
    .flex-bar{
        display: flex;
        flex-direction: row;
        align-items: center;
    }
</style>

<script>

    import request from "../utils/request";

    export default {
        name: 'Meeting',
        components: {},
        data() {
            return {
                form: {},
                dialogVisible: false,
                houseVis: false,
                search: '',
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                user: {},
                title: '',
                houseList: [],
                role: '房主',
                confirm: '确认租住',
            }
        },
        created() {
            let ownerStr = sessionStorage.getItem("user") || "{}";
            this.user = JSON.parse(ownerStr);

            //请求服务端，确认当前登录用户的合法信息
            request.get("/user/" + this.user.id).then(res => {
                if (res.code === '0') {
                    this.user = res.data;
                }
            });
            if (this.user.role === 1) {
                this.role = "租客";
            }
            if (this.user.role === 1) {
                this.confirm = "同意租出";
            }
            this.load();
        },
        methods: {
            refuse(id, houseId) {
                request.delete("/meeting/" + id).then(res => {
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "取消会面成功"
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                });
                request.delete("/wantRent/cancel/" + houseId + "/" + this.user.id).then(res => {
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "取消意向租房成功"
                        });
                        this.load();   //刷新表格的数据
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                });
            },
            showHouses(houseId){
                request.get("/house/selectHouseById", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        houseId: houseId
                    }
                }).then(res => {
                    this.houseList = res.data.records;
                    this.total = res.data.total;
                });
                this.houseVis = true;
            },
            wantRent(id) {
                request.put("/house/confirm/" + id + "/" + this.user.id).then(res => {
                    console.log(res);
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "租房成功"
                        });
                        this.load();   //刷新表格的数据
                        this.dialogVisible = false;  //关闭弹窗
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                })
            },
            cancelWantRent(id) {
                request.delete("/wantRent/cancel/" + id + "/" + this.user.id).then(res => {
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "取消请求成功"
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                    this.load();
                })
            },
            filesUploadSuccess(res) {
                console.log(res);
                this.form.photo = res.data;
            },
            load() {   //登陆的人能看到自己的见面安排表
                if (this.user.role === 1){   //登陆的是房主
                    request.get("/meeting/owner", {
                        params: {
                            pageNum: this.currentPage,
                            pageSize: this.pageSize,
                            id: this.user.id
                        }
                    }).then(res => {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    })
                }
                if (this.user.role === 2){   //登陆的是租客
                    request.get("/meeting/tenant", {
                        params: {
                            pageNum: this.currentPage,
                            pageSize: this.pageSize,
                            id: this.user.id
                        }
                    }).then(res => {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    })
                }
            },
            handleSizeChange(pageSize) {    //改变当前每页个数触发
                this.pageSize = pageSize;
                this.load();
            },
            handleCurrentChange(pageNum) {  //改变当前页码触发
                this.currentPage = pageNum;
                this.load();
            }
        }
    }
</script>